﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Routing;
using BlueLotus7.Web;

namespace BlueLotus7.Eden.Web.Models
{
    public class NavigatorItem : PageRouteInfo
    {
        protected const char splitor = '#';
        protected const char equalSign = '=';
        protected const string areaKey = "area";

        public override string Area
        {
            get
            {
                string result = string.Empty;

                if (this.OtherValues.ContainsKey(areaKey) && this.OtherValues[areaKey] != null)
                {
                    result = this.OtherValues[areaKey].ToString();
                }
                return result;
            }
            set
            {
                if (!this.OtherValues.ContainsKey(areaKey))
                {
                    this.OtherValues.Add(areaKey, string.Empty);
                }
                if (string.IsNullOrWhiteSpace(value))
                {
                    value = string.Empty;
                }
                this.OtherValues[areaKey] = value;
            }
        }

        public RouteValueDictionary OtherValues
        {
            get;
            protected set;
        }

        public int Permission
        {
            get;
            set;
        }

        public string Text
        {
            get;
            set;
        }

        public NavigatorItem()
        {
            this.OtherValues = new RouteValueDictionary();
            this.OtherValues.Add(areaKey, string.Empty);
        }

        public bool IsCurrentPage(BaseController controller)
        {
            bool result = false;

            if (controller != null)
            {
                PageRouteInfo info = Helper.GetPageRouteInfo(controller);
                result = this.EqualsUrl(info);
            }

            return result;
        }

        public string ToUrl(BaseController controller)
        {
            string result = string.Empty;

            if (controller != null)
            {
                InitliazeData();
                result = controller.Url.Action(this.Action, this.Controller, this.OtherValues);
            }

            return result;
        }

        public string ToValue()
        {
            InitliazeData();
            StringBuilder builder = new StringBuilder();

            builder.AppendFormat("{0}#{1}#{2}", this.Action, this.Controller, this.Area);

            foreach (string key in this.OtherValues.Keys)
            {
                if (key != areaKey)
                {
                    string value = this.OtherValues[key] == null ? string.Empty : this.OtherValues[key].ToString();
                    if (string.IsNullOrWhiteSpace(value))
                    {
                        value = string.Empty;
                    }
                    builder.AppendFormat("#{0}={1}", key, value);
                }
            }

            return builder.ToString();
        }

        protected void InitliazeData()
        {
            if (this.Controller == null)
            {
                this.Controller = "home";
            }

            if (this.Action == null)
            {
                this.Action = "Index";
            }
        }

        public static NavigatorItem ConvertToNavigatorItem(string exp)
        {
            NavigatorItem item = null;

            if (!string.IsNullOrWhiteSpace(exp))
            {
                item = new NavigatorItem();
                string[] parts = exp.Split(splitor);
                if (parts != null && parts.Length > 2)
                {
                    item.Action = parts[0];
                    item.Controller = parts[1];

                    if (parts.Length > 2)
                    {
                        item.Area = parts[2];
                    }

                    if (parts.Length > 3)
                    {
                        for (int i = 3; i < parts.Length; i++)
                        {
                            string[] nameValue = parts[i].Split(equalSign);
                            if (nameValue != null && nameValue.Length == 2)
                            {
                                string key = nameValue[0];
                                string value = nameValue[1];
                                if (!string.IsNullOrWhiteSpace(key))
                                {
                                    key = key.Trim();
                                    if (!item.OtherValues.ContainsKey(key))
                                    {
                                        item.OtherValues.Add(key, value);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return item;
        }
    }
}